<template>
  <div class="sysContainer">
    <div class="searchBox">
      <el-button type="primary" icon="el-icon-plus" @click="addManage">新增</el-button>
    </div>
    <BasicTable @handler="tableHandler" :column="column" :data="data"></BasicTable>
    <editInfoDialog ref="editInfoDialogRef" @reloadData="getAllManageList"></editInfoDialog>
  </div>
</template>

<script>
import BasicTable from "@/components/BasicTable/BasicTable"
import { tableColumn } from '@/constants/sysUserManageTable'
import {getManagerList, deleteManage, deleteStudent} from '@/api/userInfoManage'
import editInfoDialog from './components/editInfoDialog'

export default {
  name: "SystemUserManage",
  components: {
    BasicTable,
    editInfoDialog
  },
  data() {
    return {
      // 表头
      column: tableColumn,
      // 选中的数据
      checkList: [],
      // 数据
      data: [
        {
          id: 1,
          name: 1,
          password: '123456',
          type: 1,
          authority: 'e'
        }
      ]
    }
  },
  methods: {
    // 表格处理方法
    tableHandler(val) {
      let { type, data } = val
      console.log(type, data)
      switch(type) {
        case 'checked':
          this.checkHandler(data)
          break;
        case 'edit':
          this.editInfo(data)
          break
        case 'delete':
          this.deleteData(data)
          break
        default:
          break
      }
    },
    // 表格选中方法
    checkHandler(val) {
      this.checkList = val
      console.log(this.checkList)
    },
    // 获取所有管理员列表方法
    async getAllManageList() {
      const res = await getManagerList()
      if (res.success == true) {
        res.data.forEach(item => {
          if (item.type == 0) {
            item.typeName = '管理员'
          } else if (item.type == 1) {
            item.typeName = '教师'
          } else if (item.type == 2) {
            item.typeName = '学生'
          }
          if (item.authority == 'a') {
            item.authorityName = '所有权限'
          } else if (item.authority == 'b') {
            item.authorityName = '学生就业管理'
          } else if (item.authority == 'c') {
            item.authorityName = '学生成绩管理'
          } else if (item.authority == 'd') {
            item.authorityName = '课程信息管理'
          } else if (item.authority == 'e') {
            item.authorityName = '校园文件管理'
          } else if (item.authority == 'z') {
            item.authorityName = '没有权限'
          }
        })
        this.data = res.data
        this.pagination.total = res.data.length
      } else {
        this.$message.error(res.errorMsg)
      }
    },
    // 编辑
    editInfo(val) {
      this.$refs.editInfoDialogRef.openDialog(val)
    },
    // 删除
    async deleteData(val) {
      this.$confirm('是否删除该管理员信息？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const res = await deleteManage(val.id)
        if (res.success == true) {
          await this.getAllManageList()
          this.$message.success('删除成功!')
        } else {
          this.$message.error(res.errorMsg)
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 新增管理员
    addManage() {
      this.$refs.editInfoDialogRef.openDialog({})
    }
  },
  created() {
    this.getAllManageList()
  }
}
</script>

<style scoped lang="sass">
.searchBox
  margin-bottom: 20px
.sysContainer
  padding: 20px

</style>
